<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\association\Association as AssociationAssociation;
use app\common\model\association\Config;
use app\common\model\Config as ModelConfig;
use think\Validate;

/**
 * 协会接口
 */
class Association extends Api
{
    protected $noNeedLogin = ['introduction', 'config'];
    protected $noNeedRight = ['*'];

    /**
     * @Author LK
     * @ApiTitle (协会介绍)
     * @ApiMethod (GET)
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function introduction()
    {
        $data = [
            'advantage' => config('site.advantage'),
            'association_introduction' => config('site.association_introduction'),
            'service_scope' => config('site.service_scope'),
            'constitution' => config('site.constitution') ? cdnurl(config('site.constitution'), true) : '',
            'architecture' =>  config('site.architecture') ? cdnurl(config('site.architecture'), true) : '',
        ];
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (申请配置)
     * @ApiMethod (GET)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="1：单位入会，2：个人入会")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function config()
    {
        $param = request()->param();
        extract($param);
        if (!isset($type) || !$type) {
            $this->error('参数错误');
        }
        $data = Config::get($type);
        $this->success('', $data);
    }
    /**
     * @Author LK
     * @ApiTitle (申请入会)
     * @ApiMethod (POST)
     * @ApiParams (name="type", type="integer", required=true, sample="", description="申请类型")
     * @ApiParams (name="data", type="array", required=true, sample="", description="")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':1,'msg':'返回成功'})
     */
    public function apply()
    {
        $param = request()->param();
        extract($param);
        $validate = new Validate([
            'data'   => 'require',
            'type'  => 'require',
        ], [
            'data.require'  => '数据不能为空',
            'type.require' => '请选择类型',
        ]);
        $result = $validate->check($param);
        if (!$result) {
            $this->error(__($validate->getError()));
        }
        $param['multiplejson'] = json_encode($data);
        $param['user_id'] = $this->auth->id;
        $model = new AssociationAssociation;
        $res = $model->allowField(true)->save($param);
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
}
